package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);

        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment appointment1 = this.get(appointment.getId());

        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long id : dictIds) {
           // appointmentMapper.deleteByPrimaryKey(dictId);

            appointmentMapper.removeHideId(id,Appointment.HIDE_YES);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        Appointment appointment = this.get(id);
        Integer status = appointment.getStatus();

        if (!Appointment.STATUS_APPOINTMENT.equals(status)){
            throw new BusinessException("该用户不是预约状态");
        }else {
            appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
        }
    }

    @Override
    public void arrival(Long id) {
        Appointment appointment = this.get(id);
        Integer status = appointment.getStatus();
        if (!Appointment.STATUS_APPOINTMENT.equals(status)){
            throw new BusinessException("该用户不是预约状态");
        }else {

            appointmentMapper.userArrival(id,Appointment.STATUS_ARRIVAL);
        }




    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
        appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
        Statement statement =  generate(appointment);
        statementMapper.insert(statement);
        return statement;
    }

    private Statement generate(Appointment appointment) {
        Statement statement = new Statement();
        statement.setCreateTime(new Date());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店事件
        statement.setCarSeries(appointment.getCarSeries());//汽车类型
        statement.setCustomerName(appointment.getCustomerName());
        statement.setCustomerPhone(appointment.getCustomerPhone());
        statement.setLicensePlate(appointment.getLicensePlate());//车牌号
        statement.setAppointmentId(appointment.getId());//预约单id
        statement.setServiceType((long) appointment.getServiceType());//服务类型
        return statement;

    }


}
